﻿using System.Data.Entity;
using Quiz.Model;
using System;
using System.Linq.Expressions;
using System.Collections.Generic;
using Microsoft.WindowsAzure.Storage.Table;
using OnPremiseProductService;
using System.ServiceModel;
using Microsoft.ServiceBus;
using System.Net;
using System.ServiceModel.Channels;

namespace Quiz.Player.Models
{
    public class PlayerContext : DbContext
    {  
        // You can add custom code to this file. Changes will not be overwritten.
        // 
        // If you want Entity Framework to drop and regenerate your database
        // automatically whenever you change your model schema, add the following
        // code to the Application_Start method in your Global.asax file.
        // Note: this will destroy and re-create your database with every model change.
        // 
        // System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<Quiz.Player.Models.PlayerContext>());

        public PlayerContext() : base("name=PlayerContext")
        {
        }

        public DbSet<Question> Questions { get; set; }
        public DbSet<AnswerSet> AnswerSets { get; set; }

        public DbSet<Product> Products { get; set; }

        public IEnumerable<Product> GetProducts()
        {

            if (WebRequest.DefaultWebProxy.Credentials == null)
            {
                WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
            }

            HelliumRequest<string> cat = new HelliumRequest<string>(){  Body = "food" };
            using (IProductsChannel channel = channelFactory.CreateChannel())
            {
                channel.Open();
                var products = channel.GetProducts("food");
                return products;
            }
        }
         // Declare the channel factory
        static ChannelFactory<IProductsChannel> channelFactory;


        static PlayerContext()
        {

            //if (WebRequest.DefaultWebProxy.Credentials == null)
            //{
            //    WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
            //}

            // create the service URI based on the service namespace
            Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", "pijushbus", "ProductService");

            // create the credentials object for the endpoint
            TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior();
            sharedSecretServiceBusCredential.TokenProvider = TokenProvider.CreateSharedSecretTokenProvider("owner", "SUu1kESNR3X/YlfgSfFMBWinVX+Y5IW5Jqpqb6hBJ4k=");

            // create the channel factory loading the configuration
            channelFactory = new ChannelFactory<IProductsChannel>(new NetTcpRelayBinding(EndToEndSecurityMode.None, RelayClientAuthenticationType.RelayAccessToken), new EndpointAddress(serviceUri));

            // apply the Service Bus credentials
            channelFactory.Endpoint.Behaviors.Add(sharedSecretServiceBusCredential);
            
        }
    }
}
